/**
 * @author lk
 * @date 2022/5/31 16:14
 */
package com.mytaobao.pojo.base;

import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.persistence.*;
import java.util.Date;

@MappedSuperclass
@Data
@EqualsAndHashCode
public class BaseEntity {
    @Column(name = "create_time")
    @Temporal(TemporalType.TIMESTAMP)
    Date createTime;

    @Column(name = "update_time")
    @Temporal(TemporalType.TIMESTAMP)
    Date updateTime;

    @PrePersist
    public void perPersist() {
        Date date = new Date();
        if (updateTime == null) {
            updateTime = date;
        }
        if (createTime == null) {
            createTime = date;
        }
    }

    @PreUpdate
    public void preUpdate() {
        updateTime = new Date();
    }

    @PreRemove
    public void preRemove() {
        updateTime = new Date();
    }
}
